#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <erpc_server_setup.h>
#include "hello_server.h"

binary_t *sayHello(const binary_t *input)
{
    struct timeval t_val;
    struct tm* tm_l = NULL;
    time_t tim_t;
    size_t len = 0;
    char *buf;

    printf("ServerHello called\n");

    buf = (char *)malloc(input->dataLength + 21);
    
    strncpy(buf, (const char *)input->data, input->dataLength);

    gettimeofday(&t_val, NULL);
    tim_t = t_val.tv_sec;
    tm_l = localtime(&tim_t);
    len = strftime(&buf[input->dataLength], 21, "@%Y-%m-%d %H:%M:%S", tm_l);
    len = strlen(buf);
    printf("data: %lu; %s\n", len, buf);
    return new binary_t{(uint8_t*)buf, (uint32_t)len};
}

int main(int argc, char *argv[])
{
    erpc_transport_t transport = erpc_transport_tcp_init("127.0.0.1", 5555, true);
    erpc_mbf_t message_buffer_factory = erpc_mbf_dynamic_init();
    erpc_server_init(transport, message_buffer_factory);
    erpc_add_service_to_server(create_Hello_service());
    while(1){
        erpc_server_run();
    }
    erpc_transport_tcp_close();

    return 0;
}

